import 'package:flutter/material.dart';
import 'package:flutter_demo/widget/buttons.dart';

class C extends StatelessWidget {

  const C(this.size);

  final double size;

  @override
  Widget build(BuildContext context) {
    return SizedBox(height: size, width: size);
  }
}

class ChooseDateView extends StatefulWidget {

  const ChooseDateView(
      {Key? key,
        required this.title,
        this.callback
      }) : super(key: key);

  final String title;
  final void Function(int mill)? callback;

  @override
  State<StatefulWidget> createState() => _ChooseDateViewState();
}

class _ChooseDateViewState extends State<ChooseDateView> {

  String? date;
  
  @override
  Widget build(BuildContext context) {
    return Container(
      width: 345,
      child: Row(
        children: [
          Container(
            width: 100,
            height: 40,
            alignment: Alignment.centerRight,
            margin: EdgeInsets.only(
              left: 15,
              right: 10,
            ),
            child: Text(
              widget.title,
              textAlign: TextAlign.center,
              style: TextStyle(
                color: Color(0xff606266),
                fontSize: 14,
              ),
            ),
          ),
          Container(
            width: 132,
            height: 50,
            alignment: Alignment.center,
            child: Text(
              date ?? '请选择${widget.title}',
              textAlign: TextAlign.center,
              style: TextStyle(
                color: Color(0xff606266),
                fontSize: 14,
              ),
            ),
            decoration: BoxDecoration(
              border: Border.all(color: Colors.black26, width: 1),
              borderRadius: BorderRadius.circular(5),
            ),
          ),
          C(8),
          BaseButton(
            radius: 5,
            width: 80,
            height: 50,
            withShadow: false,
            buttonColor: Colors.blue,
            onTap: () async {
              final DateTime? dateTime = await showDatePicker(
                context: context,
                initialDate: DateTime.now(),
                firstDate: DateTime(2000, 1),
                lastDate: DateTime.now(),
              );
              if (dateTime != null && dateTime != DateTime.now()) {
                setState(() {
                  date = '${dateTime.year}年${dateTime.month}月${dateTime.day}日';
                  widget.callback!.call(dateTime.millisecondsSinceEpoch);
                });
              }
            },
            child: Text(
              '点击选择',
              style: TextStyle(
                color: Colors.white,
                fontSize: 14,
              ),
            ),
          ),
        ],
      ),
    );
  }
}